package com.jiayang.hhotel.presenter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ListView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.LevelAdapter;
import com.jiayang.hhotel.listener.OnItemClickListener;
import com.jiayang.hhotel.ui.ContractTimeAddedActivity;
import com.jiayang.hhotel.util.DateUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;

import java.util.Calendar;

import cn.droidlover.xdroidmvp.mvp.XPresent;

/**
 * Created by Administrator on 2017/5/27 0027.
 * 合同时间新增逻辑类
 */

public class ContractTimeAddedPresenter extends XPresent<ContractTimeAddedActivity> {

    private Calendar c;
    private AlertDialog dateDialog;

    /**
     * 创建日期选择器
     * @param context 上下文
     * @param isEndDate 选择的是否是终止日期
     * @param startDate 起始日期
     * @param endDate 终止日期
     */

    public void initDatePicker(final Context context, final boolean isEndDate, final String startDate,final String endDate) {
        c = Calendar.getInstance();
        final String[] date = new String[1];//选择的时间
        View pickerSelect = LayoutInflater.from(context).inflate(R.layout.can_rent_room_picker_select, null);
        final DatePicker picker = (DatePicker) pickerSelect.findViewById(R.id.dp_year);
        picker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), null);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(pickerSelect);
        builder.setNegativeButton(context.getString(R.string.determine), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int cuYear, cuMonth, cuDay;
                cuYear = picker.getYear();
                cuMonth = picker.getMonth() + 1;
                cuDay = picker.getDayOfMonth();
                String str = cuYear + "-" + StringUtil.AddZero(cuMonth) + "-" + StringUtil.AddZero(cuDay);
                if (DateUtil.TimeIsLess(str, "yyyy-MM-dd") || DateUtil.TimeIsEtc(str, "yyyy-MM-dd")) {//选择的日期是未来时间，包括当天
                    if(isEndDate){//选择的是终止日期
                        if(DateUtil.JudgeTwoDateIsEqual(startDate,str,"yyyy-MM-dd")){
                            date[0]=str;
                            getV().setEndDate(date[0]);
                        }else{
                            ToastUtil.Toast(context.getString(R.string.start_date_must_greater_than_end_date), context);
                            return;
                        }
                    }else{
                        if(!StringUtil.isEmpty(endDate)){
                            if(DateUtil.JudgeTwoDateIsEqual(str,endDate,"yyyy-MM-dd")){
                                date[0]=str;
                                getV().setStartDate(date[0]);
                            }else{
                                ToastUtil.Toast(context.getString(R.string.start_date_must_greater_than_end_date), context);
                                return;
                            }
                        }else{
                            date[0] =str;
                            getV().setStartDate(date[0]);
                        }
                    }
                } else {
                    ToastUtil.Toast(context.getString(R.string.select_time_timeout), context);
                    return;
                }
            }
        });
        dateDialog = builder.show();
    }


    private AlertDialog levelDialog;
    /**
     * 创建级别选择对话框
     * @param context 上下文
     */
    public void LevelDialog(Context context){
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        View itemView= LayoutInflater.from(context).inflate(R.layout.dialog_level,null);
        ListView listView= (ListView) itemView.findViewById(R.id.lv);
        final String[] list=context.getResources().getStringArray(R.array.time_sel_level_list);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                getV().setLevel(list[position]);
                levelDialog.dismiss();
            }
        });
        final LevelAdapter adapter=new LevelAdapter(context,list);
        listView.setAdapter(adapter);
        builder.setView(itemView);
        levelDialog = builder.show();
    }
}
